from matplotlib import gridspec
from matplotlib.axes import Axes

from src.Object import Object
import matplotlib.pyplot as plt


class MonitorView(Object):
    def __init__(self, rank, mem):
        self.rank = rank
        self.mem = mem

    def show(self):
        # with plt.xkcd():
        fig = plt.figure(figsize=(14, 9))
        # fig.canvas.mpl_connect('close_event', View.handle_close)

        gs = gridspec.GridSpec(3, 2, width_ratios=[1, 1], height_ratios=[5, 1, 3])

        plt.clf()
        ax_0: Axes = fig.add_subplot(gs[0, :])
        for index in self.rank.columns:
            ax_0.plot(self.rank[index], label=index)
        ax_0.legend()
        ax_0.tick_params(labelbottom=False)

        ax_1: Axes = fig.add_subplot(gs[1, :], sharex=ax_0)
        for index in self.mem.columns:
            ax_1.plot(self.mem[index], label=index)
        ax_1.set_ylim(550000, 800000)
        ax_1.legend()

        ax_2: Axes = fig.add_subplot(gs[2, :], sharex=ax_1)
        for index in self.mem.columns:
            ax_2.plot(self.mem[index], label=index)
        ax_2.set_ylim(0, 50000)

        # cat /sys/module/lowmemorykiller/parameters/minfree
        # cat /sys/module/lowmemorykiller/parameters/adj
        # cat /proc/2705/oom_score_adj

        ax_2.axhline(14746, linewidth=1, color='r')
        ax_2.axhline(18432, linewidth=1, color='m')
        ax_2.axhline(22118, linewidth=1, color='c')
        ax_2.axhline(25805, linewidth=1, color='y')
        ax_2.axhline(40000, linewidth=1, color='b')

        ax_1.spines['bottom'].set_visible(False)
        ax_2.spines['top'].set_visible(False)
        ax_1.xaxis.tick_top()
        ax_1.tick_params(labeltop=False)  # don't put tick labels at the top
        ax_2.xaxis.tick_bottom()

        plt.setp(ax_2.get_xticklabels(), rotation=30, ha="right")

        ax_2.annotate(
            'I AM A TEST ANNOTATE',
            xy=(2, 10000), xycoords='data', arrowprops=dict(arrowstyle='->'),
            xytext=(-15, 25), textcoords='offset points')

        plt.show()
